# 改成 HBase REST API 版本（不再使用 happybase）
import re
import json
import requests
from hdfs import InsecureClient

# HDFS 客户端连接
hdfs_client = InsecureClient('http://192.168.1.123:9870', user='root')
file_paths = hdfs_client.list('/zhenti_data')

# HBase REST 服务结构
HBASE_REST_URL = 'http://localhost:8080'
TABLE_NAME = 'tb_item'
COLUMN_FAMILY = 'info'

item_id = 1

def split_by_question_type(content):
    parts = {}
    # 完型填空
    match = re.search(r'Section I[\s\S]*?(?=Section II)', content)
    if match:
        parts['完型填空'] = match.group().strip()
    # 阅读理解
    match = re.search(r'Section II[\s\S]*?(?=Section III)', content)
    if match:
        parts['阅读'] = match.group().strip()
    # 翻译
    match = re.search(r'Section III[\s\S]*?(?=Section IV)', content)
    if match:
        parts['翻译'] = match.group().strip()
    # 写作
    match = re.search(r'Section IV[\s\S]*', content)
    if match:
        parts['写作'] = match.group().strip()
    return parts


def put_to_hbase(row_key, data):
    url = f"{HBASE_REST_URL}/{TABLE_NAME}/{row_key}"
    headers = {'Content-Type': 'application/json'}
    payload = {
        'Row': [
            {
                'key': row_key.encode('utf-8').hex(),
                'Cell': [
                    {
                        'column': f'{COLUMN_FAMILY}:type'.encode('utf-8').hex(),
                        '$': data['type'].encode('utf-8').hex()
                    },
                    {
                        'column': f'{COLUMN_FAMILY}:content'.encode('utf-8').hex(),
                        '$': data['content'].encode('utf-8').hex()
                    },
                    {
                        'column': f'{COLUMN_FAMILY}:source'.encode('utf-8').hex(),
                        '$': data['source'].encode('utf-8').hex()
                    },
                    {
                        'column': f'{COLUMN_FAMILY}:difficulty'.encode('utf-8').hex(),
                        '$': data['difficulty'].encode('utf-8').hex()
                    }
                ]
            }
        ]
    }
    r = requests.put(url, data=json.dumps(payload), headers=headers)
    if r.status_code != 200:
        print(f"写入失败: {row_key}, 错误: {r.text}")
    else:
        print(f"写入成功：{row_key}")


# 遍历所有年份文件
for fname in file_paths:
    with hdfs_client.read(f'/zhenti_data/{fname}', encoding='utf-8') as reader:
        content = reader.read()
        year = fname[:4]
        parts = split_by_question_type(content)

        for qtype, qcontent in parts.items():
            row_key = f'{item_id:06d}'
            put_to_hbase(row_key, {
                'type': qtype,
                'content': qcontent,
                'source': year,
                'difficulty': '0.5'
            })
            item_id += 1
